<!--
 * @Author: yangzhiyu yangzhiyu@mail.pigcms.com
 * @Date: 2024-03-29 16:56:47
 * @LastEditors: yangzhiyu yangzhiyu@mail.pigcms.com
 * @LastEditTime: 2024-03-29 19:40:12
 * @FilePath: \fe_user_pc\src\views\shotstack\MuseMixedCut\MixedCutPreview\components\saveVideoDialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  合成等待中
-->
<script setup>
import baseModel from "@/components/baseModel/baseModel.vue";
// import addTemplateNext from './addTemplateNext.vue'

import { ref, reactive, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
// icon
import { BellOutlined, PlusOutlined } from '@ant-design/icons-vue';
// 选择素材库视频
import ChooseMaterial from "@/views/shotstack/EditingCenter/modules/bottomSet/components/chooseMaterial.vue";
import request from '@/common/utils/request'
import { message } from 'ant-design-vue';

const props = defineProps({
    type: {
        type: Number,
    },
    // 1官方模板 0我的模板
    template_public: {
        type: Number,
        default: 0,
    }
})

const visible = ref(false);

const emit = defineEmits(['updateList'])

const state = reactive({
    material_url: ''
})

const videoUrl = ref('')

// 分级下拉数据
const options = ref([{
    label: 'Manager',
    options: [{
        value: 'jack',
        label: 'Jack',
    }, {
        value: 'lucy',
        label: 'Lucy',
    }],
}, {
    label: 'Engineer',
    options: [{
        value: 'yiminghe',
        label: 'Yiminghe',
    }],
}]);

const tagsValue = ref([])

onMounted(() => {
    getTags()
})

const getTags = async () => {
    const res = await request.get("/work_ai", "/user/shotstack/template/tags")
    if (res.result) {
        // 处理得到的标签组列表
        options.value = res.result.map((item) => {
            return {
                label: item.name,
                options: item.tag_list.map(i => {
                    return {
                        value: i.id,
                        label: i.name
                    }
                })
            }
        });
        console.log(tagsValue.value, "标签列表");
    }
}


// 选择素材库视频控制开关
const addVisible = ref(false);
// 关闭弹窗, 获取选择的文件
const closeChooseMaterial = (data) => {
    addVisible.value = false;
    console.log(data, '选择的视频');
    data && (state.material_url = data.material_url)
};

// 选择的视频素材创建方式
const radioValue = ref('1');

const deleteVideo = () => {
    state.material_url = '';
}

// 开始创建
// const addTemplateNextDialog = ref(null);
const loading = ref(false);
const StartCreating = async () => {
    console.log(props.template_public,'props.template_public');
    
    let data = {
        material_url: '',
        tab_id: tagsValue.value,
        template_public:  props.template_public,
        // material_from:+radioValue.value
    }
    if (radioValue.value == 1) {
        data.material_url = videoUrl.value
        data.split_type = split_type.value
    } else if (radioValue.value == 2) {
        data.material_url = state.material_url
        data.split_type = split_type.value
    } else {
        // 手动创建新项目
        router.push(`/mixedCutCreat?type=${props.type}&viewType=1&createType=3`)
        loading.value = false
        return
    }
    if (radioValue.value == 1 && !data.material_url) return message.error('请粘贴视频链接!');
    if (radioValue.value == 2 && !data.material_url) return message.error('请传入视频素材!');
    // 视频链接创建但是没有选择标签，不允许创建
    if ((radioValue.value == 1 || radioValue.value == 2) && tagsValue.value.length <= 0) return message.error('请选择视频标签!');

    loading.value = true
    try {
        const res = await request.post("/work_ai", "/user/shotstack/template/create/auto", data)
        if(res?.code && res.code != 1000) throw new Error(res.msg)
        loading.value = false
        router.push(`/mixedCutCreat?id=${res.result}&type=${props.type}&key=unique_id&viewType=1&createType=${radioValue.value}&templateType=${props.template_public == 1 ? 1 : 0}`)
        console.log(res, '上传结果');
        // addTemplateNextDialog.value && addTemplateNextDialog.value.showModal()
    } catch (error) {
        loading.value = false
        message.error(error.message)
    }
}

// 子弹窗关闭
const closeModelState = (type) => {
    // debugger
    if (type == 'back') {
        // 返回模板广场
        emit('updateList')
        videoUrl.value = ''
        state.material_url = ''
        tagsValue.value = []
        closeModal()
    } else {
        // 清空数据,继续新建 
        videoUrl.value = ''
        state.material_url = ''
        tagsValue.value = []
    }
}

// 清空当前组件内数据
const clearData = () => {
    // console.log('清空数据');
    videoUrl.value = ''
    state.material_url = ''
    tagsValue.value = []
    radioValue.value = '1'
    split_type.value = 0
}



const showModal = (activeKey) => {
    visible.value = true
};
const closeModal = () => {
    // clearData()
    visible.value = false
}

watch(() => visible.value, (nv, ov) => {
    if (!nv) {
        clearData()
    } 
})

// 视频拆分类型
const split_type = ref(0)
defineExpose({
    showModal,
    closeModal
});
</script>

<template>
    <div>
            <a-modal v-model:visible="visible" :maskClosable="false" width="720px">
            <template #title>
                <div class="title_box">
                    <span class="title">添加模板</span>
                    <span class="tips">
                        <BellOutlined />
                        提示：添加视频素材可用于模板创建参与
                    </span>
                </div>
            </template>
            <!-- <template #content> -->
                <div class="content">
                    <!-- {{ tagsValue }} -->

                    <div class="radio_box">
                        <span class="radio_title">视频来源:</span>
                        <a-radio-group v-model:value="radioValue" name="radioGroup">
                            <a-radio value="1">网络视频</a-radio>
                            <a-radio value="2">素材库视频</a-radio>
                            <a-radio value="3">手动创建</a-radio>
                        </a-radio-group>
                    </div>
                    <div class="update_content">
                        <div class="tags_select" v-show="radioValue == 1 || radioValue == 2">
                            <span class="tags_title">视频标签:</span>
                            <a-select v-model:value="tagsValue" mode="multiple" :max-tag-text-length="590"
                                style="width: 100%" @change="handleChange" placeholder="请选择视频标签" allowClear
                                :options="options">
                            </a-select>
                        </div>
                        <div class="template_split_type" v-if="radioValue != '3'">
                            <span class="template_split_type_title">拆分类型:</span>
                            <a-radio-group v-model:value="split_type">
                                <a-radio :value="0">按视频场景拆分</a-radio>
                                <a-radio :value="1">按口播音频拆分</a-radio>
                            </a-radio-group>
                        </div>
                        <div class="video_link" v-show="radioValue == 1">
                            <span class="link_title">视频链接:</span>
                            <a-textarea v-model:value="videoUrl" placeholder="请粘贴抖音短视频链接, 视频时长不超过20分钟" :rows="4" />
                        </div>
                        <div class="video_upload" v-show="radioValue == 2">
                            <span class="video_title">添加素材:</span>
                            <div v-if="!state.material_url" class="selectVideo" @click="addVisible = true">
                                <span class="selectVideo_content">
                                    <PlusOutlined />
                                    添加素材
                                </span>
                            </div>
                            <div class="video_con" v-else>
                                <video id="videoPlayer" :src="state.material_url" autoplay />
                                <div class="operate_con">
                                    <cu-icon @click="deleteVideo" type="icon-shanchu1" class="delete_icon" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <!-- </template> -->
            <template #footer>
                <div class="bottom">
                    <a-button @click="closeModal">取消</a-button>
                    <a-button @click="StartCreating" type="primary" :loading="loading">开始创建</a-button>
                </div>
            </template>
        </a-modal>

        <!-- </baseModel> -->

        <!-- 选择 素材库视频 -->
        <ChooseMaterial :isClip="false" material_type="video" :visible="addVisible" @close="closeChooseMaterial" />
        <!-- <addTemplateNext ref="addTemplateNextDialog" @closeModelState:closeType="closeModelState" :template_public="template_public"></addTemplateNext> -->
    </div>
</template>



<style lang="less" scoped>
@fontColor: #1c1f23;

.title_box {
    display: flex;
    flex-direction: column;

    .title {
        font-size: 14px;
        font-weight: 600;
    }

    .tips {
        font-size: 12px;
        color: #aaaaaa;
        margin-top: 10px;
    }
}

.content {
    display: flex;
    flex-direction: column;
    // align-items: center;

    .radio_box {
        display: flex;
        align-items: center;

        .radio_title {
            margin-right: 15px;
        }
    }

    .update_content {
        padding-top: 20px;

        .video_link {
            display: flex;

            // align-items: center;
            .link_title {
                margin-right: 15px;
                white-space: nowrap;
            }
        }

        .tags_select {
            display: flex;
            margin-bottom: 20px;

            .tags_title {
                margin-right: 15px;
                white-space: nowrap;
            }
        }

        .video_upload {
            display: flex;

            .video_title {
                margin-right: 15px;
                white-space: nowrap;
            }

            // 选择视频
            .selectVideo {
                height: 120px;
                width: 120px;
                background-color: #f1f3f5;
                display: flex;
                align-items: center;
                justify-content: center;
                cursor: pointer;

                .selectVideo_content {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    font-size: 14px;
                }
            }

            // 视频容器
            .video_con {
                width: 150px;
                height: 250px;
                background-color: #000;
                display: flex;
                align-items: center;
                justify-content: center;
                position: relative;

                .operate_con {
                    display: none;
                }

                &:hover {
                    .operate_con {
                        width: 150px;
                        height: 250px;
                        position: absolute;
                        left: 0;
                        top: 0;
                        background-color: rgba(0, 0, 0, .3);
                        display: flex;
                        align-items: center;
                        justify-content: center;

                        .play_icon {
                            color: #fff;
                            font-size: 24px;
                            cursor: pointer;

                            &:hover {
                                color: #ff5652;
                            }
                        }

                        .delete_icon {
                            color: #fff;
                            font-size: 24px;
                            cursor: pointer;

                            &:hover {
                                color: #ff5652;
                            }
                        }
                    }
                }

                video {
                    max-width: 100%;
                    max-height: 100%;
                }
            }
        }

        .template_split_type {
            display: flex;
            margin: 10px 0;

            .template_split_type_title {
                margin-right: 15px;
                white-space: nowrap;
            }
        }
    }
}

.bottom {
    display: flex;
    justify-content: center;
}
</style>